package com.story.web.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContext;

import org.springframework.web.context.ServletContextAware;

public class ServerPathExposer implements ServletContextAware {
	private final static String WEBNAME = "红红小博客啊";
	private final static String VERSION = "v1.0";
	public String resRoot;
	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	public void init(){
		String webName = "";
		String version = "";
		try {
			InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/conf/webconfig.properties");
			Properties prop = new Properties();
			prop.load(in);
			webName = prop.getProperty("webName");
			version = prop.getProperty("version");
			if(webName.trim().equals("")){
				webName = WEBNAME;
			}
			if(version.trim().equals("")){
				version = VERSION;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		resRoot = "/res-" + version.trim();
		getServletContext().setAttribute("base",getServletContext().getContextPath());
		getServletContext().setAttribute("res", getServletContext().getContextPath()+resRoot);
		getServletContext().setAttribute("webName", webName.trim());
	}

	public ServletContext getServletContext() {
		return servletContext;
	}
	
	
}
